<?php
/**
 * DBShop 电子商务系统
 *
 * ==========================================================================
 * @link      https://www.dbshop.net/
 * @copyright 北京珑大钜商科技有限公司，并保留所有权利。
 * @license   https://www.dbshop.net/license.html License
 * ==========================================================================
 *
 * @author    静静的风 <baron@loongdom.cn>
 *
 */

namespace Shop\Validator;

use Laminas\I18n\Translator\Translator;
use Laminas\Validator\AbstractValidator;

class UserWithdrawMoneyValidator extends AbstractValidator
{
    const NOT_SCALAR    = 'notScalar';
    const MONEY_MIN     = 'moneyMin';
    const MONEY_MAX     = 'moneyMax';
    const FEE_ERROR     = 'feeError';

    protected $messageTemplates = [];

    protected $userInfo;
    protected $withdrawFee;
    public function __construct($options = null)
    {
        if(isset($options['userInfo']))    $this->userInfo = $options['userInfo'];
        if(isset($options['withdrawFee'])) $this->withdrawFee = $options['withdrawFee'];

        $trans = new Translator();
        $this->messageTemplates = [
            self::NOT_SCALAR=> $trans->translate("这不是一个标准输入值"),
            self::MONEY_MIN => $trans->translate("提现金额太小，请重新输入"),
            self::MONEY_MAX => $trans->translate("提现金额超过账户余额，无法进行提现处理"),
            self::FEE_ERROR => $trans->translate("余额中没有足够的手续费扣除"),
        ];

        parent::__construct($options);
    }

    public function isValid($value)
    {
        if(!is_scalar($value)) {
            $this->error(self::NOT_SCALAR);
            return false;
        }

        if($value <= 0) {
            $this->error(self::MONEY_MIN);
            return false;
        }

        if ($this->withdrawFee > 0 && ($value + $value * $this->withdrawFee * 0.01) > $this->userInfo->getUserMoney()) {
            $this->error(self::FEE_ERROR);
            return false;
        }

        $isValid = true;

        if($value > $this->userInfo->getUserMoney()) {
            $isValid = false;
            $this->error(self::MONEY_MAX);
        }

        return $isValid;
    }
}